﻿#pragma once
#include<iostream>
using namespace std;
//红黑树：二叉搜索树+4条红黑树规则
// 1.红黑树所有节点有且只有两种颜色：红、黑
// 2.红黑树的根必须为黑色
// 3.红色节点的孩子必须为黑色
// 4.对于任意一个节点，从这个节点开始到NULL节点的黑色节点数量都是一样的


// 枚举值表⽰颜⾊ 
enum Colour
{
	RED,
	BLACK,
};

// 这里我们默认按key/value结构实现 
template<class T>
struct RBTreeNode
{
	// 这⾥更新控制平衡也要加⼊parent指针 
	T _kv;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;

	RBTreeNode(const T& kv)//const?
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		,_col(RED)
	{}
};

template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> Self;

	Node* _node;
	Node* _root;

	RBTreeIterator(Node* node,Node* root)
		:_node(node)
		,_root(root)
	{
	}


	// 完善迭代器的++操作，让迭代器可以移动
	Self& operator++()
	{
		//如果右不为null，找右树的最左节点
		if(_node->_right)
		{
			Node* cur = _node->_right;
			while (cur->_left)
			{
				cur = cur->_left;
			}
			_node = cur;
		}
		else//右为空，则去找孩子是父亲左边的祖先
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && parent->_right== cur)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}

		return *this;
	}

	//与++的逻辑相反
	Self& operator--()
	{
		//当为end()时，返回最右节点
		if (_node == nullptr)
		{
			Node* cur = _root;
			while (cur &&cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		//如果左子树不为空,则去找左子树的最右节点
		else if (_node->_left)
		{
			Node* cur = _node->_left;
			while (cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		else//如果左子树为空, 则去找孩子是父亲右的祖先
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while(parent&&parent->_left==cur)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}

		return *this;
	}


	// 完善下面两个操作，让迭代器可以像指针一样操作
	T& operator*()
	{
		return _node->_kv;
	}

	T* operator->()//返回指针
	{
		return &_node->_kv;
	}

	// 完善下面两个操作，让迭代器能够支持比较
	bool operator!=(const Self& s)const
	{
		return _node != s._node;
	}

	bool operator==(const Self& s)const
	{
		return _node == s._node;
	}
};

template<class K, class V,class KetOfT>
class RBTree
{
	//typedef RBTreeNode<V> Node;
	using Node = RBTreeNode<V>;
public:
	typedef RBTreeIterator<V, V&, V*> Iterator;
	typedef RBTreeIterator<V, const V&, const V*> ConstIterator;

	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur,_root);//调用构造函数
	}

	Iterator End()
	{
		return Iterator(nullptr, _root);
	}

	ConstIterator Begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return ConstIterator(cur, _root);//调用构造函数
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr, _root);
	}

	KetOfT kot;
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}


	pair<Iterator,bool> Insert(const V& kv)
	{
		Node* cur = _root;
		Node* parent = cur;

		if (!cur)//如果是空树
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			//return pair<Iterator, bool>(Iterator(_root, _root), true);
			return  { Iterator(_root, _root), true };
			
		}

		while(cur)
		{
			if (kot(cur->_kv) > kot(kv))
			{
				parent = cur;
				cur = parent->_left;
			}
			else if (kot(cur->_kv) < kot(kv))
			{
				parent = cur;
				cur = parent->_right;
			}
			else
			{
				return  { Iterator(cur, _root), false };
			}
		}
		cur = new Node(kv);
		if (kot(parent->_kv) > kot(kv))
			parent->_left = cur;
		else
			parent->_right = cur;

		//连接至上一层
		cur->_parent = parent;

		Node* newNode = cur;
		//判断是否满足条件
		while (parent&&parent->_col == RED)
		{
			Node* pparent = parent->_parent;

			//先讨论parent在左边的情况
			if (pparent->_left == parent)
			{
				Node* uncle = pparent->_right;
				//uncle存在且为红 -> 变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					pparent->_col = RED;

					cur = pparent;
					parent = cur->_parent;
				}
				else
				{
					//单旋+变色
					if (parent->_left == cur)
					{
						RotateR(pparent);
						pparent->_col = RED;
						parent->_col = BLACK;
						break;
					}
					//双旋+变色
					else
					{
						RotateL(parent);
						RotateR(pparent);
						pparent->_col = RED;
						cur->_col = BLACK;
						break;
					}
				}
			}
			else//再讨论parent在右边的情况
			{
				Node* uncle = pparent->_left;
				//uncle存在且为红 -> 变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					pparent->_col = RED;

					cur = pparent;
					parent = cur->_parent;
				}
				else
				{
					//单旋+变色
					if (parent->_right == cur)
					{
						RotateL(pparent);
						parent->_col = BLACK;
						pparent->_col = RED;
						break;
					}
					else//双旋+变色
					{
						RotateR(parent);
						RotateL(pparent);
						cur->_col = BLACK;
						pparent->_col = RED;
						break;
					}
				}
			}
			_root->_col = BLACK;
		}
		return  { Iterator(newNode, _root), true};
	}

	//右单旋
	void RotateR(Node* parent)
	{
		Node* pparent = parent->_parent;
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		if(subLR)
			subLR->_parent = parent;
		parent->_left = subLR;
		
		subL->_right = parent;
		parent->_parent = subL;

		if (!pparent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				subL->_parent = pparent;
				pparent->_left = subL;
			}
			else
			{
				subL->_parent = pparent;
				pparent->_right = subL;
			}
		}
	}

	//左单旋
	void RotateL(Node* parent)
	{
		Node* pparent = parent->_parent;
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		subR->_left = parent;
		parent->_parent = subR;

		if(!pparent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subR;
				subR->_parent = pparent;
			}
			else
			{
				pparent->_right = subR;
				subR->_parent = pparent;
			}
		}
	}

private:
	
	Node* _root = nullptr;
};